import inquirer from "inquirer";
import { comps } from "./data";
import { compUtils } from "./utils";
//
import {QS,getCompSortInfo,dictPlugDemos,dictCompDemos} from "./questions";
//

const QD = QS.D;
const QC = QS.comp
const QG = QS.compGroup
const QE = QS.compDemo

const addComp = async () => {
  let {groupName} = await inquirer.prompt([QC.group]);
  if(groupName == "new"){
    const groupData = await addCompGroup();
    groupName = groupData.name;
  }
  let {name,label,desc} = await inquirer.prompt([QC.name,QD.label,QD.desc])
  //
  let {sort} = await inquirer.prompt([{...QD.sort,...getCompSortInfo(groupName)}])
  const {menu} = await inquirer.prompt([{...QD.menu,default:label}])
  const {isAddDemo} = await inquirer.prompt([QD.isAddDemo])
  const data = {name,label,desc,sort,menu,group:groupName,demos:[]};
  compUtils.add(data,isAddDemo)
  if(isAddDemo){
    await addCompDemo(data)
  }
  return data;
}

const delComp = async () => {
  const { name,isDelete } = await inquirer.prompt([QC.select,QD.isDelete]);
  if(isDelete){
    compUtils.del(name)
  }
}

const addCompGroup = async () => {
  const { parent } = await inquirer.prompt([QG.parent]);
  //
  const {name,label,desc} = await inquirer.prompt([QG.name,QD.label,QD.desc])
  const {sort} = await inquirer.prompt([{...QD.sort,...getCompSortInfo(parent,true)}])
  const data = {name,label,desc,parent,sort}
  compUtils.addGroup(data)
  return data
}

const delCompGroup = async () => {
  const { name,isDelete } = await inquirer.prompt([QG.select,QD.isDelete]);
  if(isDelete){
    compUtils.delGroup(name)
  }
}

const addCompDemo = async (d) => {
  let t = d;
  if(!d){
    const { name:n } = await inquirer.prompt([QC.select]);
    t = comps[n]
  }
  const {name,label,desc} = await inquirer.prompt([QE.name,QD.label,QD.desc])
  const {sort} = await inquirer.prompt([{...QD.sort,default:t.demos.length}])
  //
  const data = {name,label,desc,upper:t.name,group:t.group,sort}
  //
  compUtils.addDemo(data)
  //
  return data;
}

const delCompDemo = async () => {
  const { name:n } = await inquirer.prompt([QC.select]);
  const choices = dictCompDemos.filter(d => comps[n].demos.includes(d.value))
  const { name,isDelete } = await inquirer.prompt([{...QE.select,choices},QD.isDelete]);
  if(isDelete){
    compUtils.delDemo(name)
  }
}

const refreshCompData = async () => {
  const { name:n } = await inquirer.prompt([QC.select]);
  if(n){
    compUtils.refreshMain(comps[n]);
  }
}

const refreshCompDocs = async () => {
  const { name:n } = await inquirer.prompt([QC.select]);
  if(n){
    compUtils.refreshDocs(comps[n]);
  }
}

const refreshCompMd = async () => {
  const { name:n } = await inquirer.prompt([QC.select]);
  if(n){
    compUtils.refreshMd(comps[n]);
  }
}

const refreshCompDemos = async () => {
  const { name:n } = await inquirer.prompt([QC.select]);
  if(n){
    compUtils.refreshDemos(comps[n]);
  }
}

const refreshComp = async () => {
  const { name:n } = await inquirer.prompt([QC.select]);
  if(n){
    compUtils.refresh(comps[n]);
  }
}

const refreshCompsIndex = async () => {
  const { isReset } = await inquirer.prompt([QD.isReset]);
  if(isReset){
    compUtils.refreshIndex()
  }
}

export default {
  addComp,
  delComp,
  addCompGroup,
  delCompGroup,
  addCompDemo,
  delCompDemo,
  refreshCompData,
  refreshCompMd,
  refreshCompDemos,
  refreshCompDocs,
  refreshCompsIndex,
  refreshComp
}